package com.gpStudy.Netty.IO.BIO.tomcat_netty.http;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;

public class GPResponse {

    private ChannelHandlerContext ctx;

    private HttpRequest req;

    public GPResponse(ChannelHandlerContext ctx, HttpRequest req) {
        this.ctx = ctx;
        this.req = req;
    }

    public void write(String content) throws Exception{

        try {
            if (null == content || content.length() == 0) {
                return;
            }
            FullHttpResponse response = new DefaultFullHttpResponse(
                    // http 1.1
                    HttpVersion.HTTP_1_1,
                    // 响应状态码
                    HttpResponseStatus.OK,
                    // 将输出值写出  编码为 utf-8
                    Unpooled.wrappedBuffer(content.getBytes("UTF-8")));
            response.headers().set("Content-Type", "text/html");
            // 当前是否支持 长链接
/*            if (HttpUtil.isKeepAlive(response)) {
                response.headers().set("Connection", "Keep-Alive");
            }*/
            ctx.write(response);

        } finally {
            ctx.flush();
            ctx.close();
        }
    }
}
